<?php
class Shopping_Form_Validate_Product_Edit{
	
	//CHUA NHUNG THONG BAO LOI CUA FORM
	protected $_messagesError = null;
	
	//MANG CHUA DU LIEU SAU KHI KIEM TRA
	protected  $_arrData;
	
	public function __construct($arrParam = array(), $options = null){
		
		//=========================================
		//KIEM TRA name
		//==========================================
		$validator = new Zend_Validate();
		$clause    = 'id != '. $arrParam['id'];
		$option    = array('table'=>'products','field'=>'name','exclude'=>$clause);
		$validator->addValidator(new Zendvn_Validate_NotEmpty(),true)
				  ->addValidator(new Zendvn_Validate_Db_NoRecordExists($option),true);
		if(!$validator->isValid($arrParam['name'])){
			$message = $validator->getMessages();
			$this->_messagesError['name'] = 'Tên Sách: '.current($message);
			$arrParam['name']='';
		}
		
		//=========================================
		//KIEM TRA  picture
		//==========================================
		$upload = new Zend_File_Transfer_Adapter_Http();
		$fileInfo  = $upload->getFileInfo('picture');
		$fileName  = $fileInfo['picture']['name'];
		if(!empty($fileName)){
			$upload->addValidator('Extension',true,array('jpg','png','gif'),'picture')
				   ->addValidator('Size',true,array('min'=>'2KB','max'=>'1000KB'),'picture');
			if(!$upload->isValid('picture')){
				$message = $upload->getMessages();
				$this->_messagesError['picture']= 'Hình ảnh:' . current($message);
			}
		}
		//=========================================
		//KIEM TRA author
		//==========================================
		$validator = new Zend_Validate();
		$validator->addValidator(new Zendvn_Validate_NotEmpty(),true)
				 ->addValidator(new Zendvn_Validate_StringLength(4,100),true);
		if(!$validator->isValid($arrParam['author'])){
				$message = $validator->getMessages();
				$this->_messagesError['author'] = 'Tác giả: '.current($message);
				$arrParam['author']='';
		}
		
		//=========================================
		//KIEM TRA price
		//==========================================
		$validator = new Zend_Validate();
		$validator->addValidator(new Zendvn_Validate_NotEmpty(),true)
				  ->addValidator(new Zendvn_Validate_Digits(),true);
		if(!$validator->isValid($arrParam['price'])){
			$message = $validator->getMessages();
			$this->_messagesError['price'] = 'Giá bán: '.current($message);
			$arrParam['price']='';
		}
		
		//=========================================
		//KIEM TRA selloff
		//==========================================
		$validator = new Zend_Validate();
		$validator->addValidator(new Zendvn_Validate_NotEmpty(),true)
				  ->addValidator(new Zendvn_Validate_Digits(),true)
				  ->addValidator(new Zendvn_Validate_Between(0,$arrParam['selloff']),true);
		if(!$validator->isValid($arrParam['selloff'])){
			$message = $validator->getMessages();
			$this->_messagesError['selloff'] = 'Giá khuyến mại: '.current($message);
			$arrParam['selloff']='';
		}
		
		//=========================================
		//KIEM TRA Status
		//==========================================
		if($arrParam['status'] == 2){
			$this->_messagesError['status'] = 'Chưa chọn trạng thái';
		}
		
		//=========================================
		//KIEM TRA special
		//==========================================
		if($arrParam['special'] == 2){
			$this->_messagesError['special'] = 'Chưa chọn kiểu sản phẩm';
		}
		
		//=========================================
		//KIEM TRA publish_date	
		//==========================================
		if($arrParam['day'] == 0){
			$this->_messagesError['day'] = 'Chưa chọn ngày sinh';
			$arrParam['day'] = 0;
		}
			
		if($arrParam['month'] == 0){
			$this->_messagesError['month'] = 'Chưa chọn tháng sinh';
			$arrParam['month'] = 0;
		}
		if($arrParam['year'] == 0){
			$this->_messagesError['year'] = 'Chưa chọn năm sinh';
			$arrParam['year'] = 0;
		}
		
		//=========================================
		//KIEM TRA Order
		//==========================================
		$validator = new Zend_Validate();
		$validator->addValidator(new Zendvn_Validate_NotEmpty(),true)
					->addValidator(new Zendvn_Validate_Digits(),true);
		if(!$validator->isValid($arrParam['order'])){
			$message = $validator->getMessages();
			$this->_messagesError['order'] = 'Giá trị: '.current($message);
			$arrParam['order']='';
		}
		//=========================================
		//KIEM TRA $content
		//==========================================
		$validator = new Zend_Validate();
		$validator->addValidator(new Zendvn_Validate_NotEmpty(),true)
		->addValidator(new Zendvn_Validate_StringLength(10),true);
		if(!$validator->isValid($arrParam['content'])){
			$message = $validator->getMessages();
			$this->_messagesError['content'] = 'Nội dung: '.current($message);
			$arrParam['content']='';
		}
		
		//=========================================
		//KIEM TRA cat_id
		//==========================================
		if($arrParam['cat_id'] == 0){
			$this->_messagesError['cat_id'] = 'Thuộc mục : chưa chọn mục';
		}
		
		//=========================================
		//TRUYEN CAC GIA TRI DUNG VAO $_arrData
		//==========================================
		$this->_arrData = $arrParam;
	}
	
	//KIEM TRA LOI
	//return true neu co loi xay ra
	public function isError(){
		if(count($this->_messagesError)>0){
			return true;
		}else{
			return false;
		}
		
	}
	
	//TRA VE MOT MANG CAC LOI
	public function getMessageError(){
		return $this->_messagesError;
	}
	
	//TRA VE MOT MANG DU LIEU SAU KHI KIEM TRA
	public function getData($options = null){
	 if($options == 'upload'){
	 	$this->_arrData['picture'] = $this->uploadFile();
	 }
		return $this->_arrData;
	}
	
	public function uploadFile(){
		$upload = new Zendvn_File_Upload();
		$fileInfo   = $upload->getFileInfo('picture');
		$fileName   = $fileInfo['picture']['name'];
		if(!empty($fileName)){
			$fileName = $upload->upload('picture',FILES_PATH . '/product/orignal/',
										array('task'=>'rename'),'product_'
										);
			$thumb = Zendvn_File_Image::create(FILES_PATH . '/product/orignal/'.$fileName);
			$thumb->resize(82,122)
				  ->save(FILES_PATH . '/product/img82x122/'.$fileName);
				  
			$thumb = Zendvn_File_Image::create(FILES_PATH . '/product/orignal/'.$fileName);
			$thumb->resize(57,84)
			       ->save(FILES_PATH . '/product/img57x84/'.$fileName);
			
			$thumb = Zendvn_File_Image::create(FILES_PATH . '/product/orignal/'.$fileName);
			$thumb->resize(200,300)
				  ->save(FILES_PATH . '/product/img200x300/'.$fileName);
			
			$upload->removeFile(FILES_PATH . '/product/orignal/'. $this->_arrData['current_picture']);
			$upload->removeFile(FILES_PATH . '/product/img82x122/' . $this->_arrData['current_picture']);
			$upload->removeFile(FILES_PATH . '/product/img57x84/'. $this->_arrData['current_picture']);
			$upload->removeFile(FILES_PATH . '/product/img200x300/'. $this->_arrData['current_picture']);
		}else{
			$fileName = $this->_arrData['current_picture'];
		}
		return $fileName;
	}
}